﻿using ArtMath.Core.Data;
using ArtMath.Flag;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace ArtMath.AuxiliaryTool
{
    /// <summary>
    /// 通用矩形选择器
    /// </summary>
    public class RectangularPicker : RectangleDragger
    {
        ArrayList selectedBackup;
        bool dobkp = true;
        IObjectPickable pick;
        public RectangularPicker(IObjectPickable pick, PointD initLocation) : base(initLocation)
        {
            this.pick = pick;
        }
        public override void MouseMove(MouseMoveEventArgs e)
        {
            base.MouseMove(e);
            switch (e.Button)
            {
                case MouseButtons.Left:
                case MouseButtons.Right:
                    if (dobkp && Flags.Ctrl)
                    {
                        dobkp = false;
                        if (selectedBackup == null)
                        {
                            selectedBackup = new ArrayList();
                        }
                        foreach (object item in pick.SelectedObjects)
                        {
                            selectedBackup.Add(item);
                        }
                    }
                    if (e.CurrentPosition == initRect.Location) return;
                    pick.SetObjectSelectionState(InitialRectangle.ToStandardRectangle());
                    break;
            }
        }
        public override void Complete()
        {
            if (selectedBackup != null)
            {
                selectedBackup.Clear();
                dobkp = true;
            }
            base.Complete();
        }
        public bool GetInitialSelection(object obj)
        {
            return selectedBackup.Contains(obj);
        }
        public override void Paint(Graphics gra)
        {
            SmoothingMode bkp = gra.SmoothingMode;
            gra.SmoothingMode = SmoothingMode.HighSpeed;
            RectangleF rect = InitialRectangle.ToStandardRectangle().ToRectangleF();
            gra.FillRectangle(new SolidBrush(Color.FromArgb(100, 170, 204, 238)), rect);
            gra.DrawRectangle(new Pen(Color.FromArgb(0, 120, 215), 1), rect.Left, rect.Top, rect.Width, rect.Height);
            gra.SmoothingMode = bkp;
        }
    }
}
